package cn.itcast.web.controller.company;


import cn.itcast.domain.company.Company;
import cn.itcast.service.company.CompanyService;
import cn.itcast.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController {

 @Autowired
 private CompanyService companyService;

 @RequiresPermissions(value = "企业管理")
 @RequestMapping("/list")
 public String findAll(
  @RequestParam(defaultValue = "1") int page,
  @RequestParam(defaultValue = "10") int size
 ) {
  //调用service查询分页页数
  //PageResult pr = companyService.findPage(page,size);

  /*List<Company> list = companyService.findAll();*/

  PageInfo info = companyService.findPageByHelper(page,size);

  request.setAttribute("page", info);

  return "company/company-list";
 }

 /**
  * 进入保存企业页面
  */
 @RequestMapping("/toAdd")
 public String toAdd() {
  return "company/company-add";
 }

 /**
  * 保存企业
  */
 @RequestMapping("/edit")
 public String edit(Company company) {
  //判断company对象中是否存在id
  if (StringUtils.isEmpty(company.getId())) {
   //不存在，调用保存
   companyService.save(company);
  } else {
   //存在，调用更新
   companyService.update(company);
  }
  return "redirect:/company/list.do";
 }

 /**
  * 根据id查询
  * 进入修改页面
  */
 @RequestMapping("/toUpdate")
 public String toUpdate(String id) {
  Company company = companyService.findById(id);
  request.setAttribute("company", company);
  return "company/company-update";
 }

 /**
  * 根据id删除
  */
 @RequestMapping("/delete")
 public String delete(String id) {
  companyService.delete(id);
  return "redirect:/company/list.do";
 }
}
